package com.example.demo.service.user;

import com.example.demo.commom.AjaxResult;
import com.example.demo.commom.PasswordUtils;
import com.example.demo.commom.TokenUtil;
import com.example.demo.dto.user.login.ULoginDto;
import com.example.demo.dto.user.reg.PRegDto;
import com.example.demo.dto.user.reg.URegDto;
import com.example.demo.entity.user.Admin;
import com.example.demo.entity.user.Parent;
import com.example.demo.entity.user.Teacher;
import com.example.demo.mapper.user.AdminMapper;
import com.example.demo.mapper.user.ParentMapper;
import com.example.demo.vo.user.LoginVo;
import com.example.demo.vo.user.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-31
 * Time:18:37
 */
@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;
    public Integer reg(URegDto regDto) {
        // 密码加密
        regDto.setPassword(PasswordUtils.encrypt(regDto.getPassword()));
        if(adminMapper.getAdminByCardId(regDto.getCard_id()) != null){
            return 0;
        }
        //注册
        return adminMapper.reg(regDto);

    }

    // 登录
    public LoginVo login(ULoginDto uLoginDto) {
        //校验是否可以进行登录操作
        Admin admin = adminMapper.getAdminById(uLoginDto.getUser_id());
        if (admin == null || admin.getAdmin_id() < 1 || admin.getAdmin_id() > 1999) {
            return LoginVo.returnInfo(-1, "管理员编号或密码错误");
        }
        if (admin.getState() > 2 && admin.getUnfreezing_time() > System.currentTimeMillis()) {
            Long waitTime = (admin.getUnfreezing_time() - System.currentTimeMillis()) / 1000;
            return LoginVo.returnInfo(-1, "请等待"+waitTime+"s后重试");
        }
        if (admin.getState() > 2 && admin.getUnfreezing_time() <= System.currentTimeMillis()) {
            admin.setState(0);
            adminMapper.upAdminState(admin);
        }
        //密码加盐处理过
        if (PasswordUtils.check(uLoginDto.getPassword(), admin.getPassword())) {
            admin.setState(0);
            adminMapper.upAdminState(admin);
            return LoginVo.returnInfo(0, TokenUtil.getToken(admin.getAdmin_id()));
        } else {
            //1、先将state修改
            admin.setState(admin.getState() + 1);
            //2、是否修改取消冻结的最新时间
            if (admin.getState() > 2) {
                admin.setUnfreezing_time(System.currentTimeMillis() + 20000);//当前时间往后家100s
            }
            //更新state+unfreezingtime
            adminMapper.upAdminState(admin);
            return LoginVo.returnInfo(-1, "管理员或密码输入错误");
        }
    }

    // 验证管理员身份
    public Boolean authentication(Integer user_id){
        Admin admin = adminMapper.getAdminById(user_id);
        if (admin == null || admin.getAdmin_id() < 1 || admin.getAdmin_id() > 1999) {
            return false;
        }
        return true;
    }

    // 查看管理员信息
    public List<UserInfoVo> getAdminInfo(Integer admin_id){
        List<UserInfoVo> infos = new ArrayList<>();
        if(admin_id == 0){
            // 查看所有管理员信息
            List<Admin> admins = adminMapper.getAllAdmin();
            for(int i = 0;i<admins.size();i++){
                infos.add(UserInfoVo.createVoByAdmin(admins.get(i)));
            }
            return infos;
        } else {
            // 查看指定管理员信息
            Admin admin = adminMapper.getAdminById(admin_id);
            if(admin == null){
                return null;
            }
            infos.add(UserInfoVo.createVoByAdmin(admin));
            return infos;
        }
    }
}
